package com.qinggeng.project.system.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.qinggeng.common.exception.CustomException;
import com.qinggeng.common.utils.StringUtils;
import com.qinggeng.common.utils.text.Convert;
import com.qinggeng.project.api.security.utils.ApiUtils;
import com.qinggeng.project.system.domain.UserMap;
import com.qinggeng.project.system.domain.json.UserMapIdConfig;
import com.qinggeng.project.system.mapper.UserMapMapper;
import com.qinggeng.project.system.service.ISysConfigService;
import com.qinggeng.project.system.service.ISysUserService;
import com.qinggeng.project.system.service.IUserMapService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

/**
 * 用户导航栏Service业务层处理
 *
 * @author smallfish
 * @date 2020-12-07
 */
@Service
public class UserMapServiceImpl implements IUserMapService
{

    public static final String USER_MAP_JOB_ID="sys.usermap.jobId";

    public static final String USER_MAP_ID_KEY="sys.usermap.id";

    public static final String USER_DISTRICT_KEY="USER_DISTRICT:";
    @Resource
    private UserMapMapper userMapMapper;
    @Autowired
    private ISysConfigService sysConfigService;
    @Autowired
    private ISysUserService sysUserService;

    /**
     * 查询用户导航栏
     *
     * @param id 用户导航栏ID
     * @return 用户导航栏
     */
    @Override
    public UserMap selectUserMapById(Long id)
    {
        return userMapMapper.getUserMapById(id);
    }

    /**
     * 查询用户导航栏列表
     *
     * @param
     * @return 用户导航栏
     */
    @Override
    public List<UserMap> listUserMap(String district,UserMap where)
    {

       Long jobId= Convert.strToLong(sysConfigService.getConfigValue(USER_MAP_JOB_ID),7L);
        //showType=2仅团长可见
        where.setShowType(sysUserService.checkIsChief()?2:1);
        List<UserMap> userMapList=userMapMapper.listUserMap(where);
        for (UserMap userMap :userMapList  ) {
            if(userMap.getId().compareTo(jobId)!=0){
                continue;
            }
            if(StringUtils.isEmpty(district)){
                userMap.setName("同城"+userMap.getName());
            }else {
                if(district.length()>2){
                    district=district.substring(0,district.length()-1);
                }
                userMap.setName(district+userMap.getName());
            }
            break;
        }
        return userMapList;
    }

    @Override
    public List<UserMap> listUserMap(UserMap userMap) {
        return userMapMapper.listUserMap(userMap);
    }

    /**
     * 新增用户导航栏
     *
     * @param userMap 用户导航栏
     * @return 结果
     */
    @Override
    public int insertUserMap(UserMap userMap)
    {
        return userMapMapper.insertUserMap(userMap);
    }

    /**
     * 修改用户导航栏
     *
     * @param userMap 用户导航栏
     * @return 结果
     */
    @Override
    public int updateUserMap(UserMap userMap)
    {
        return userMapMapper.updateUserMap(userMap);
    }

    /**
     * 批量删除用户导航栏
     *
     * @param ids 需要删除的用户导航栏ID
     * @return 结果
     */
    @Override
    public int deleteUserMapByIds(Long[] ids)
    {
        return userMapMapper.deleteUserMapByIds(ids);
    }

    /**
     * 删除用户导航栏信息
     *
     * @param id 用户导航栏ID
     * @return 结果
     */
    @Override
    public int deleteUserMapById(Long id)
    {
        return userMapMapper.deleteUserMapById(id);
    }
}
